﻿using System;
using _5E.Infrastructure.Domain;
using WholesaleOnline.Model.BusinessRules;

namespace WholesaleOnline.Model
{
    partial class WoBasketItem : EntityBase<Guid>, IAggregateRoot
    {
        private WoProduct m_Product = null;
        public virtual WoProduct Product
        {
            get { return m_Product; }
            set
            {
                if (null != value)
                    ProductId = value.Id;
                m_Product = value;
            }
        }

        private WoBasket m_Basket = null;
        public virtual WoBasket Basket
        {
            get { return m_Basket; }
            set
            {
                if (null != value)
                    BasketId = value.Id;
                m_Basket = value;
            }
        }

        public virtual decimal TotalPrice
        {
            get { return Product.Price * Quantity; }
        }

        public WoBasketItem()
        {
            Id = Guid.NewGuid();
        }

        public WoBasketItem(WoProduct product, Int32 qty, WoBasket basket)
            : this()
        {
            Product = product;
            Quantity = qty;
            Basket = basket;
        }

        public virtual bool Contains(WoProduct product)
        {
            return Product == product;
        }

        public virtual void IncreaseItemQtyBy(Int32 qty)
        {
            Quantity += qty;
        }

        public virtual void ChangetItemQtyTo(Int32 qty)
        {
            Quantity = qty;
        }

        protected override void Validate()
        {
            if (null == Product)
                base.AddBrokenRule(BasketItemBusinessRules.ProductInvalid);
        }
    }
}
